package com.lcxw.hxzbadmin.controller.goodnamecontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbadmin.entity.requestbean.AddGoodNameReq;
import com.lcxw.hxzbadmin.entity.requestbean.DeleteGoodNameReq;
import com.lcxw.hxzbadmin.entity.requestbean.GetGoodNamesReq;
import com.lcxw.hxzbadmin.entity.requestbean.GoodNameInfo;
import com.lcxw.hxzbadmin.entity.requestbean.RequestBodyBasic;
import com.lcxw.hxzbapi.controller.goodnamecontroller.GoodNameRealization;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbapi.entity.pojo.GoodName;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbcommon.common.StringUtil;

@RestController
@RequestMapping("admin/goodName")
public class GoodNameController {

    @Resource
    GoodNameRealization goodNameRealization;

    @PostMapping("/add")
    public Response addGoodName(@RequestBody AddGoodNameReq addGoodNameReq) {
        List<GoodNameInfo> goodNameInfos = addGoodNameReq.getGoodNameInfos();
        if (goodNameInfos.size() > 100) {
            return Response.error(ErrorCode.NOT_SUPPLY, "添加靓号超过最大个数,不能超过100个");
        }
        List<GoodName> goodNames = new ArrayList<>();
        for (GoodNameInfo goodNameInfo : goodNameInfos
        ) {
            GoodName goodName = new GoodName();
            BeanUtils.copyProperties(goodNameInfo, goodName);
            goodName.setSold(0);
            goodName.setAddTime(StringUtil.getDateTime());
            int length = goodNameInfo.getName().length();
            goodName.setLength(length);
            goodNames.add(goodName);
        }

        return goodNameRealization.addGoodNames(goodNames);
    }

    @PostMapping("/getAll")
    public Response getAllGoodNames(@Valid @RequestBody GetGoodNamesReq getGoodNamesReq) {
        int pageNum = getGoodNamesReq.getPageNum();
        int pageSize = getGoodNamesReq.getPageSize();
        Integer sold = getGoodNamesReq.getSold();
        return goodNameRealization.getAll(sold, pageNum, pageSize);
    }

    @PostMapping("/delete")
    public Response deleteGoodName(@RequestBody DeleteGoodNameReq deleteGoodNameReq) {
        List<Integer> deleteIdList = deleteGoodNameReq.getDeleteIdList();
        return goodNameRealization.deleteGoodNames(deleteIdList);
    }


}
